package com.jxtti.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jxtti.entity.AdvisoryMsg;
import com.jxtti.entity.ArticleList;
import com.jxtti.model.District;
import com.jxtti.model.User;
import com.jxtti.service.AdvisoryMsgService;
import com.jxtti.service.ArticleListService;
import com.jxtti.service.DistrictService;
import com.jxtti.utils.AuthUtil;
import com.jxtti.utils.Constants;

/**
 * 后台 首页 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/AdminDesktop") 
public class Admin_Desktop_Controller {
	
private final static Logger log= Logger.getLogger(Admin_Menu_Controller.class);
	
	@InitBinder  //初始化数据的时候，进行数据类型转换，String类型转为Date类型
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:���������ֵ��false:����Ϊ��ֵ 
    }
	
	@Autowired
	private AdvisoryMsgService  advisoryMsgService; 
	
	@Autowired 
	private ArticleListService  articleListService; 
	
	@Autowired
	private DistrictService districtService;
	
	//后台descktop.htm
	@RequestMapping(value = "/DesktopInterface")
	@ResponseBody
	public ModelAndView TopInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
			User userInfo = AuthUtil.getUserInfo(request);
			
			//查找最新留言			
			AdvisoryMsg advisoryMsg=new AdvisoryMsg();
			Map<String, Object> advisorydata=new HashMap<String, Object>();
			advisorydata.put("is_deleted", "0");			
			advisorydata.put("pageSize","5");//分页5条		
			//判断是否管理员			
			if(data.get("noAdmin")!=null && data.get("noAdmin").equals("yes")){ //普通管理员无效
				//超管权限为7
				if(userInfo.getRoleId()!=Constants.AdminRole){ //是否超管--不是  加上权限
					advisorydata.put("states","1");//已分配
					advisorydata.put("task_org_id",userInfo.getOrgId()); 	
				}else{
					advisorydata.put("states","0");//待回复
				}
			}else{
				if(userInfo.getDistrictId()==null || userInfo.getDistrictId().equals("0")){ //非管理员
					advisorydata.put("states","1");//已分配
					advisorydata.put("task_org_id",userInfo.getOrgId()); 
				}else{
					advisorydata.put("states","0");//待回复
				}
			}			
			advisorydata=advisoryMsgService.listDesk(advisorydata,advisoryMsg);			
			data.put("advisorydata",advisorydata);
			
			Map<String, String> userData=new HashMap<String, String>();
			userData.put("id", userInfo.getOrgId().toString());
			List<District>  dis=districtService.queryByList(userData);

			if(userInfo.getRoleId()!=com.jxtti.utils.Constants.AdminRole){ //是否超管--不是  加上权限
				if(dis!=null && dis.size()>0){
					if(dis.get(0).getParentId().toString().trim().equals("3607")){ //局科室
						data.put("isDept","true");					
					}else{
						data.put("isDept","false");	
					}
				}
			}
			
			//查找最新待审列表			
			Map<String, Object> articledata=new HashMap<String, Object>();
			articledata.put("is_contribute", "1");//县级投稿			
			articledata.put("topNum","5");		
			//判断是否管理员			
			if(data.get("noAdmin")!=null && data.get("noAdmin").equals("yes")){ //普通管理员无效
				if(userInfo.getRoleId()!=com.jxtti.utils.Constants.AdminRole){ //是否超管--不是  加上权限
					articledata.put("fk_orgId",userInfo.getOrgId()); 	
				}else{
					articledata.put("status",1);//待审核
				}
			}else{
				if(userInfo.getDistrictId()==null || userInfo.getDistrictId().equals("0")){ //非管理员					
					articledata.put("fk_orgId",userInfo.getOrgId()); 
				}else{
					articledata.put("status",1);//待审核
				}
			}	
			List<ArticleList> articleList=articleListService.queryListForIndex(articledata);
			data.put("articleList",articleList);
						
			
			mav.addObject("data", data);		
			mav.setViewName("/pages/Admin/Desktop.jsp");
		return mav;
	}
		

}
